package com.demo2do.fms.admin.web.controller.supplier.sub.event;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.supplier.TractorEventSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.SupplierService;
import com.demo2do.fms.admin.service.supplier.TractorEventService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.supplier.common.Supplier;
import com.demo2do.fms.entity.vehicle.tractor.TractorEvent;

/**
 * 分属供方牵引车日志查询
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/sub-tractor-events")
@MenuSetting("menu-supplier")
public class SubTractorEventController {

    @Autowired
    private SupplierService supplierService;
    
    @Autowired
    private TractorEventService tractorEventService;

    /**
     * list
     * 
     * @param login
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-sub-tractor-event")
    public ModelAndView list(@Secure Login login, HttpServletRequest request, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/sub/event/tractor/sub-tractor-event-list");
        
        List<Long> supplierIds = supplierService.listSubSupplierId(login);
        
        int totals = tractorEventService.count(supplierIds);
        if(totals > 0) {
            
            List<TractorEvent> events = tractorEventService.list(supplierIds, page.with(totals));
            modelAndView.addObject("events", events);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }

        return modelAndView;
        
    }
    
    /**
     * search panel
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search(@Secure Login login) {

        ModelAndView modelAndView = new ModelAndView("supplier/sub/event/tractor/sub-tractor-event-search");
        
        List<Supplier> suppliers = supplierService.listSubSupplier(login);
        modelAndView.addObject("suppliers", suppliers);

        return modelAndView;
        
    }

    /**
     * search
     * 
     * @param login
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-sub-tractor-event")
    public ModelAndView onSearch(@Secure Login login, TractorEventSearch search, 
                                 HttpServletRequest request, Page page) {


        ModelAndView modelAndView = new ModelAndView("supplier/sub/event/tractor/sub-tractor-event-list");

        List<Long> supplierIds = supplierService.listSubSupplierId(login);
        search.addSupplierId(supplierIds);
        
        int totals = tractorEventService.count(search);
        if (totals > 0) {
            
            List<TractorEvent> events = tractorEventService.list(search, page.with(totals));
            modelAndView.addObject("events", events);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }

        return modelAndView;
        
    }
    
}
